<template>
	<view>
		<view class="myUpload">
			<view class="upload_box" @click="album_check" :style="{ width: `${up_width}rpx`, height: `${up_width}rpx` }">
				<view class="upload_view">
					<image :src="upurl ? upurl : url" class="upload_pic" mode="aspectFill" @error="imgError"></image>
					<view class="upload_press" v-if="upload_press_show == 1">{{ upload_press }}%</view>
				</view>
			</view>
		</view>

		<myAuthorityPopup :on="authorityPopup" :popup_config="authorityPopup_config" @popup_btn_click="authority_click($event)"></myAuthorityPopup>
	</view>
</template>

<script>
import storeMixin from '@/store/storeMixin';
export default {
	mixins: [storeMixin],
	name: 'myUpload',
	props: {
		key_name: {
			type: [String],
		},
		url: {
			type: [String],
			default() {
				return '';
			},
		},
		upload_type: {
			type: [String],
			default() {
				return '';
			},
		},
		item: {
			type: [Object],
			default() {
				return {};
			},
		},
		up_width: {
			type: [String, Number],
			default() {
				return '90';
			},
		},
	},
	watch: {},
	data() {
		return {
			upload_press_show: 0,
			upload_press: 0,
			upurl: '',
			choose_list: [],

			// 权限授权弹窗
			authorityPopup: false,
			authorityPopup_config: {
				system_key: 'photoLibrary',
			},
		};
	},
	mounted() {},
	methods: {
		imgError(e) {
			// debugger
		},
		chooseImage() {
			let _this = this;
			//图片上传
			uni.chooseImage({
				// mediaType: ['image'],
				sourceType: ['album'],
				count: 1, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				success: function (res) {
					// for (let item of res.tempFiles) {
					// 	_this.choose_list.push(item.path)
					// }
					_this.choose_list = res.tempFilePaths;
					_this.upload_now(res.tempFilePaths);

					// _this.upload_F(res.tempFilePaths, 0);
					console.log(_this.choose_list, '_this.choose_list');
					// console.log(res, 'res')
				},
				fail(err) {
					console.log(err);
				},
			});
		},

		album_check() {
			let _this = this;
			console.log('album_check');
			_this.check_permission({
				k: _this.authorityPopup_config.system_key,
				suc_fun: function () {
					_this.chooseImage();
				},
				fail_fun: function () {
					_this.popup_show(1);
				},
			});
		},

		popup_show(type) {
			if (type == 1) {
				this.authorityPopup = true;
			} else if (type == -1) {
				this.authorityPopup = false;
			}
		},
		authority_click(event) {
			let _this = this;
			let { type, system_key, key } = event;
			// console.log(type, system_key, key);
			if (type == 1) {
				_this.popup_show(-1);
				_this.authority_click_callback(system_key, key, flag => {
					// 业务函数
					if (flag) {
						_this.chooseImage();
					} else {
						// 拒绝授权返回
						// _this.com_page_back();
					}
				});
				return;
			} else {
				// 拒绝授权返回
				_this.popup_show(-1);
				// _this.com_page_back();
			}
		},

		upload_now(list) {
			let _this = this;
			for (let i = 0; i < list.length; i++) {
				_this.upload_ele(list[i]);
			}
		},
		async upload_ele(path) {
			let _this = this;
			let { data = false } = await _this.com_upload({
				type: 'head',
				path,
			});
			if (data) {
				_this.upurl = data;
				_this.$emit('upload_end', {
					value: data,
					key: _this.key_name,
					item: _this.item,
				});
			}
			// let str = await _this.base64_en(url);
			// let type = this.upload_type;
			// _this.com_getdata({
			// 	url: '/base/upload',
			// 	data_handle_on: false,
			// 	data: {
			// 		type,
			// 		str,
			// 	},
			// 	data_fun: function (v) {
			// 		if (v.state == 1) {
			// 			_this.upurl = v.data;
			// 			_this.$emit('upload_end', {
			// 				value: v.data,
			// 				key: _this.key_name,
			// 				item: _this.item,
			// 			});
			// 		} else {
			// 			_this.com_msg(v.msg || '网络请求错误');
			// 		}
			// 	},
			// });
		},

		formTrigger(e, item) {
			console.log(e, 'ee');
			let _this = this;
			if (!this.formupdate) return;
			this.formupdate = false;

			let { state } = item;
			if (state == 'edit') {
				if (_this.objJudge(item.regular)) {
					if (!_this.page_check(e.detail.value, item.regular)) return;
				} else {
					if (!e.detail.value) {
						return _this.com_msg(item.placeholder);
					}
				}
			}
			let o = {
				value: e.detail.value,
				key: item.key,
			};
			this.$emit('formChange', o);
			this.$nextTick(() => {
				_this.formupdate = true;
			});
		},
	},
};
</script>

<style lang="scss">
.upload_view {
	width: 100%;
	height: 100%;

	.upload_pic {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}
}
</style>
